﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HFController;
namespace HFWebApp.legislation
{
    public partial class LegislationList : BagePages
    {
        public string PageStr = "";
        public string ClassTitle = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int PageSize = 8;
                int PageNumber = 1;
                if (this.GetQuery("Page") != "")
                {
                    PageNumber = int.Parse(this.GetQuery("Page"));
                }
                int total = 0;

                #region 搜索条件
                string strWhere = "";

                #region 分类
                if (this.GetQuery("classid") != "")
                {
                    int classid = int.Parse(this.GetQuery("classid"));
                    if (classid != 0)
                    {
                        strWhere = "LegislationClassID=" + classid.ToString();
                        ClassTitle = LegislationController.getController().getLegislationClassModel(classid).ClassName;
                    }
                }
                #endregion

                #region 区域
                if (this.GetQuery("provinceid") != "")
                {
                    int provinceid = int.Parse(this.GetQuery("provinceid"));
                    if (provinceid != 0)
                    {
                        if (strWhere == "")
                        {
                            strWhere = "ProvinceID = " + provinceid.ToString();
                            ClassTitle = CityController.getController().getProvinceList().Find(i => i.ID == provinceid).ProvinceName;
                        }
                        else
                        {
                            strWhere = strWhere + " and ProvinceID = " + provinceid.ToString();
                        }
                    }
                }
                #endregion
                //LegislationList.aspx?keyword=&libid=0&classid=0&provinceid=0&status=0&addtime=&effectivetime=&searchtype=0&x=27&y=23
                #region 类库分类ID
                if (this.GetQuery("libid") != "")
                {
                    int libid = int.Parse(this.GetQuery("libid"));
                    if(libid != 0)
                    {
                        if (strWhere == "")
                        {
                            strWhere = "LegislationLibaryID = " + libid.ToString();
                            ClassTitle = LegislationController.getController().getLegislationLibaryModel(libid).LegislationLibaryName;
                        }
                        else {
                            strWhere = strWhere +" and LegislationLibaryID = " + libid.ToString();
                        }
                    }
                }
                #endregion
                
                #region 关键字
                string searchtype = this.GetQuery("searchtype");
                if (this.GetQuery("keyword") != "")
                {
                    string keyword = this.GetQuery("keyword");
                    if (searchtype == "0")
                    {
                        if (strWhere == "")
                        {
                            strWhere = "Title like '%" + keyword.ToString() + "%' " ;
                            ClassTitle = "关键字搜索";
                        }
                        else
                        {
                            strWhere = strWhere + "and Title like '%" + keyword.ToString() + "%' ";
                        }
                    }else{
                        if (strWhere == "")
                        {
                            strWhere = "Content like '%" + keyword.ToString() + "%' ";
                            ClassTitle = "关键字搜索";
                        }
                        else
                        {
                            strWhere = strWhere + "and Content like '%" + keyword.ToString() + "%' ";
                        }
                    }
                }
                #endregion

                #region 状态

                if (this.GetQuery("status") != "0" && this.GetQuery("status") != "")
                {
                    int status = int.Parse(this.GetQuery("status"));
                    if (strWhere == "")
                    {
                        strWhere = "StatusID = " + status.ToString();
                        ClassTitle = this.GetLegislationStatus(status.ToString());
                    }
                    else
                    {
                        strWhere = strWhere + "and StatusID = " + status.ToString();
                    }
                }

                #endregion

                #region 时间范围
                if (this.GetQuery("addtime") != "" && this.GetQuery("effectivetime") != "")
                {
                    string addtime = this.GetQuery("addtime");
                    string effectivetime = this.GetQuery("effectivetime");
                    if (strWhere == "")
                    {
                        strWhere = "Addtime > '" + addtime + "' and Effectivetime  < '" + effectivetime + "'";
                        ClassTitle = "时间范围查询";
                    }
                    else
                    {
                        strWhere = "and Addtime > '" + addtime + "' and Effectivetime < '" + effectivetime + "'";
                    }
                }
                #endregion

                #endregion

                Repeater1.DataSource = LegislationController.getController().getLegislationList(PageSize, PageNumber, strWhere, out total);
                Repeater1.DataBind();

                PageStr = LLL.Pager.TrunPageHtmlCode_Web(PageSize, PageNumber, total, "");
            }
        }
    }
}